#include <iostream>
#include <cerrno>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <cstring>
#include <sys/socket.h>
#include "Log.hpp"
#include "Thread.hpp"
#include "InetAddr.hpp"
#include "Comm.hpp"

using namespace std;

void Usage(string proc)
{
    cout << "Usage: \n\t" << proc << "local_ip local_port\n" << endl;
}

class ThreadData
{
public:
    ThreadData(int sockfd, struct sockaddr_in &serveraddr)
        :_sockfd(sockfd)
        ,_serveraddr(serveraddr)
    {}

    ~ThreadData()
    {}
public:
    int _sockfd;
    InetAddr _serveraddr;
};

void RecverRoutine(ThreadData &td)
{
    while(true)
    {
        // 收消息
            char buffer[1024];
            struct sockaddr_in temp;
            socklen_t len = sizeof(temp);
            ssize_t n = recvfrom(td._sockfd, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&temp, &len);
            if(n > 0)
            {
                buffer[n] = 0;
                cerr << "get message: " << buffer << endl;
            }
            else 
                break;
    }
}

void SenderRoutine(ThreadData &td)
{
    while(true)
    {
        // 发数据
        string inbuffer;
        cout << "Please Enter# ";
        getline(cin, inbuffer);

        auto server = td._serveraddr.GetAddr();
        // 发送目标
        ssize_t n = sendto(td._sockfd, inbuffer.c_str(), inbuffer.size(), 0, (struct sockaddr*)&server, sizeof(server));
        if(n <= 0)
        {
            cout << "send error" << endl;
        }
    }
}

// ./udp_server server_ip server_port
int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        return Usage_Err;
    }
    string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);

    // 创建socket
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0)
    {
        lg.LogMessage(Fatal, "socket err, %d : %s", errno, strerror(errno));
        exit(Socket_Err);
    }
    lg.LogMessage(Info, "socket success, sockfd: %d\n", sock);

    // cilent 也需要进行bind,但是不需要显示bind,让本地OS自动随机bind,选择随机端口号
    // 填充server信息
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    ThreadData td(sock, server);
    Thread<ThreadData> recver("recver", RecverRoutine, td);
    Thread<ThreadData> sender("sender", SenderRoutine, td);
    recver.Start();
    sender.Start();

    recver.Join();
    sender.Join();

    close(sock);
    return 0;
}








// while(true)
//     {
//         // 发数据
//         string inbuffer;
//         cout << "Please Enter# ";
//         getline(cin, inbuffer);
//         // 发送目标
//         ssize_t n = sendto(sock, inbuffer.c_str(), inbuffer.size(), 0, (struct sockaddr*)&server, sizeof(server));
//         if(n > 0)
//         {
//             // 收消息
//             char buffer[1024];
//             struct sockaddr_in temp;
//             socklen_t len = sizeof(temp);
//             ssize_t m = recvfrom(sock, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&temp, &len);
//             if(m > 0)
//             {
//                 buffer[m] = 0;
//                 cout << "server echo# " << buffer << endl;
//             }
//             else 
//                 break;
//         }
//         else
//             break;
//     }